package com.bingxue.edu.management.education.controller;

import com.bingxue.edu.framework.common.model.PageQuery;
import com.bingxue.edu.framework.common.model.Pagination;
import com.bingxue.edu.framework.common.model.Response;
import com.bingxue.edu.framework.common.model.SortQuery;
import com.bingxue.edu.framework.mybatis.query.QueryBuilder;
import com.bingxue.edu.framework.security.ApiResource;
import com.bingxue.edu.management.education.model.entity.Classroom;
import com.bingxue.edu.management.education.model.query.ClassroomQuery;
import com.bingxue.edu.management.education.model.req.ClassroomReq;
import com.bingxue.edu.management.education.model.resp.ClassroomResp;
import com.bingxue.edu.management.education.service.ClassroomService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 教室接口
 *
 * @author panbo
 * @since 2024/07/17 19:02
 */
@Tag(name = "教室接口")
@RestController
@RequestMapping("/system/classroom")
@RequiredArgsConstructor
public class ClassroomController {

    private final ClassroomService classroomService;

    @Operation(summary = "查询列表")
//    @ApiResource("system:org:manage")
    @GetMapping("/list")
    public Response<List<ClassroomResp>> list(ClassroomQuery query, SortQuery sortQuery) {
        QueryWrapper queryWrapper = QueryBuilder.build(query, sortQuery);
        List<ClassroomResp> list = classroomService.getMapper().selectListWithRelationsByQueryAs(queryWrapper, ClassroomResp.class);
        return Response.success(list);
    }

    @Operation(summary = "分页查询列表")
//    @ApiResource("system:org:manage")
    @GetMapping("/page")
    public Response<Pagination<ClassroomResp>> page(ClassroomQuery query, @Validated PageQuery pageQuery) {
        QueryWrapper queryWrapper = QueryBuilder.build(query, pageQuery);
        Page<ClassroomResp> page = classroomService.pageAs(pageQuery.toPage(), queryWrapper, ClassroomResp.class);
        return Response.success(Pagination.of(page));
    }

    @Operation(summary = "查询详情")
//    @ApiResource("system:org:manage")
    @GetMapping("/detail/{id}")
    public Response<ClassroomResp> detail(@PathVariable String id) {
        ClassroomResp detail = classroomService.getMapper().selectOneWithRelationsByIdAs(id, ClassroomResp.class);
        return Response.success(detail);
    }

    @Operation(summary="创建数据")
    @ApiResource("system:org:manage")
    @PostMapping("/create")
    public Response<Void> create(@RequestBody @Validated ClassroomReq req) {
        Classroom classroom = classroomService.copy(req);
        classroomService.save(classroom);
        return Response.success();
    }

    @Operation(summary="更新数据")
    @ApiResource("system:org:manage")
    @PostMapping("/update/{id}")
    public Response<Void> update(@RequestBody @Validated ClassroomReq req, @PathVariable String id) {
        Classroom classroom = classroomService.copyById(req, id);
        classroomService.updateById(classroom);
        return Response.success();
    }

    @Operation(summary="删除数据")
    @ApiResource("system:org:manage")
    @PostMapping("/delete/{ids}")
    public Response<Void> delete(@PathVariable List<String> ids) {
        classroomService.removeByIds(ids);
        return Response.success();
    }

}
